package com.ljpz.manage.controller;

import com.ljpz.manage.pojo.Item;
import com.ljpz.manage.pojo.ItemCat;
import com.ljpz.manage.service.ItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * Created by liujunchen on 2016/11/27.
 */
@Controller
@RequestMapping("item/cat")
public class ItemCatController {
    @Autowired
     private ItemCatService   itemCatService  ;
    /**
     *
     * 查询商品类目类别
     * @param parentid
     * @return
     */
     @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<List<ItemCat>> queryItemCat(
        @RequestParam(value = "id",defaultValue = "0") Long parentid ) {
         try {
             ItemCat    record = new ItemCat()  ;
             record.setParentId(parentid);
             List<ItemCat> itemCats = this.itemCatService.queryListByWhere(record);
             if (itemCats == null || itemCats.isEmpty())
             //404查询没有返回对象或者失败
             {
                 return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
             }
             return ResponseEntity.ok(itemCats);
         } catch (Exception e) {
             e.printStackTrace();
         }
         return  ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null)    ;
     }
     @RequestMapping(  value = "{id}", method = RequestMethod.GET)
     public ResponseEntity<ItemCat>queryItemCatById(@PathVariable(value = "id")Long  id ){
        try {
            ItemCat itemCat = this.itemCatService.queryById(id);
            if (itemCat == null)
            //404查询没有返回对象或者失败
            {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
            }
            return ResponseEntity.ok(itemCat);
        }catch ( Exception e ){
            e.printStackTrace();
        }
        return   ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null)   ;
     }

}
